﻿<?php
/**
 * File keep RSSGenerator class 
 *
 * @author Andrey Kucherenko
 */
 
//test
 
/**
 * RSSGenerator class 
 *
 */
class Apk_RSSGenerator
{
    /**
     * XMLWriter object
     *
     * @var XMLWriter
     */
    public $xmlWriter;

    /**
     * Make RSSGenerator object
     *
     * @param string $encoding
     */
    public function __construct($encoding = 'utf-8')
    {
        $this->xmlWriter = new XMLWriter();
        $this->xmlWriter->openMemory();
        $this->xmlWriter->startDocument('1.0', $encoding);
    }

    /**
     * Start RSS document
     *
     */
    public function startDocument()
    {
        $this->xmlWriter->startElement('rss');
        $this->xmlWriter->writeAttribute('version', '2.0');
    }

    /**
     * Close rss tag, use only if document started
     *
     */
    public function endDocument()
    {
        $this->xmlWriter->endElement();
    }

    /**
     * Start channel tag
     *
     */
    public function startChannel()
    {
        $this->xmlWriter->startElement('channel');
    }

    /**
     * Close channel, use only if channel tag started
     *
     */
    public function endChannel()
    {
        $this->xmlWriter->endElement();
    }

    /**
     * Start item tag
     *
     */
    public function startItem()
    {
        $this->xmlWriter->startElement('item');
    }

    /**
     * Close item tag, use only if item started
     *
     */
    public function endItem()
    {
        $this->xmlWriter->endElement();
    }

    /**
     * Start image tag
     *
     */
    public function startImage()
    {
        $this->xmlWriter->startElement('image');
    }

    /**
     * Close image tag, use only image tag started
     *
     */
    public function endImage()
    {
        $this->xmlWriter->endElement();
    }

    /**
     * Write title tag to RSS
     *
     * @param unknown_type $value
     */
    public function title($value)
    {
        $this->xmlWriter->writeElement('title', $value);
    }

    /**
     * Add link tag to rss 
     *
     * @param string $value
     */
    public function link($value)
    {
        $this->xmlWriter->writeElement('link', $value);
    }

    /**
     * Add pubDate tag to rss
     *
     * @param unknown_type $value
     */
    public function pubDate($value)
    {
        if (is_int($value)){
            $this->xmlWriter->writeElement('pubDate', date('r', $value));
        }else{
            $this->xmlWriter->writeElement('pubDate', $value);
        }
    }

    /**
     * Add description tag to RSS
     *
     * @param string $value
     */
    public function description($value)
    {
        $this->xmlWriter->startElement('description');
        $this->xmlWriter->writeCdata($value);
        $this->xmlWriter->endElement();
    }

    /**
     * Write custom element to RSS document
     *
     * @param string $name
     * @param string $value
     */
    public function writeElement($name, $value)
    {
        $this->xmlWriter->writeElement($name, $value);
    }

    /**
     * Return result RSS xml 
     *
     * @return string
     */
    public function dump()
    {
        return $this->xmlWriter->outputMemory();
    }

    /**
     * Send resulted rss xml & headers to browser
     *
     */
    public function output()
    {
         header( "Content-type: text/xml");
         echo $this->dump();
         die();
    }
    
    /**
     * Save rss xml to file 
     *
     * @param string $filename
     */
    public function save($filename)
    {
        $fp = fopen($filename, 'w');
        fwrite($fp, $this->xmlWriter->outputMemory());
        fclose($fp);
    }
    
     public function saveutf8bom($filename)
    {
        $fp = fopen($filename, 'wb');
        fwrite($fp, chr(239).chr(187).chr(191).$this->xmlWriter->outputMemory());
        fclose($fp);
    }    
}
